<template>
	<xn-form-container
		:title="formData.id ? '编辑试卷表' : '增加试卷表'"
		:width="1500"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="试卷分类：" name="categoryId">
				<a-tree-select
					v-model:value="formData.categoryId"
					class="xn-wd"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择所属题库分类"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
					selectable="false"
					tree-line
				/>
			</a-form-item>
			<a-form-item label="试卷名称：" name="paperName">
				<a-input v-model:value="formData.paperName" placeholder="请输入试卷名称" allow-clear />
			</a-form-item>

			<a-form-item label="选题模式：" name="selectionMode">
				<a-select v-model:value="formData.selectionMode" placeholder="请选择选题模式" :options="selectionModeOptions" />
			</a-form-item>
			<a-form-item label="选题配置：" name="selectionConfig">
				<Random
					v-if="formData.selectionMode === 'random'"
					:selectionConfig="formData.selectionConfig"
					@updateSelectionConfig="(val) => (formData.selectionConfig = val)"
					@updateTotalScore="(val) => (formData.totalScore = val)"
					@updateQuestionCount="(val) => (formData.questionCount = val)"
				/>
				<Fixed
					v-if="formData.selectionMode === 'fixed'"
					:selectionConfig="formData.selectionConfig"
					@updateSelectionConfig="(val) => (formData.selectionConfig = val)"
					@updateTotalScore="(val) => (formData.totalScore = val)"
					@updateQuestionCount="(val) => (formData.questionCount = val)"
				/>
			</a-form-item>
			<a-form-item label="题目数量：" name="questionCount">
				<a-input-number
					v-model:value="formData.questionCount"
					placeholder="请输入题目数量"
					:min="1"
					:max="10000"
					style="width: 100%"
					disabled
				/>
			</a-form-item>
			<a-form-item label="试卷总分：" name="totalScore">
				<a-input-number
					v-model:value="formData.totalScore"
					placeholder="请输入试卷总分"
					:min="1"
					:max="10000"
					style="width: 100%"
					disabled
				/>
			</a-form-item>
			<a-form-item label="考试限时/分钟：" name="examTimeLimit">
				<a-input-number
					v-model:value="formData.examTimeLimit"
					placeholder="请输入考试限时/分钟"
					:min="1"
					:max="10000"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="每日限制考试次数：" name="dailyAttemptLimit">
				<a-input-number
					v-model:value="formData.dailyAttemptLimit"
					placeholder="请输入每日限制考试次数"
					:min="1"
					:max="10000"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="使用开始时间：" name="startDate">
				<a-date-picker
					v-model:value="formData.startDate"
					value-format="YYYY-MM-DD HH:mm:ss"
					show-time
					placeholder="请选择使用开始时间"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="使用结束时间：" name="endDate">
				<a-date-picker
					v-model:value="formData.endDate"
					value-format="YYYY-MM-DD HH:mm:ss"
					show-time
					placeholder="请选择使用结束时间"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="仅用于考场：" name="onlyForExamHall">
				<a-select
					v-model:value="formData.onlyForExamHall"
					placeholder="请选择仅用于考场"
					:options="onlyForExamHallOptions"
				/>
			</a-form-item>
			<a-form-item label="封面图片：" name="coverImage">
				<xn-upload v-model:value="formData.coverImage" uploadMode="image" />
			</a-form-item>
			<a-form-item label="状态：" name="status">
				<a-select v-model:value="formData.status" placeholder="请选择状态" :options="statusOptions" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="tblExamPaperForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import tblExamPaperApi from '@/api/biz/tblExamPaperApi'
	import tblCategoryApi from '@/api/biz/tblCategoryApi'
	import Random from '@/views/biz/examPaper/random.vue'
	import Fixed from '@/views/biz/examPaper/fixed.vue'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const selectionModeOptions = ref([])
	const onlyForExamHallOptions = ref([])
	const statusOptions = ref([])
	const treeData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		selectionModeOptions.value = tool.dictList('exam_paper_selection_mode')
		onlyForExamHallOptions.value = tool.dictList('yes_no')
		statusOptions.value = tool.dictList('status')
		tblCategoryApi.categoryTreeSelector({ type: 'paper' }).then((res) => {
			treeData.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		categoryId: [{ required: true, message: '试卷分类不能为空' }],
		paperName: [{ required: true, message: '试卷名称不能为空' }],
		selectionMode: [{ required: true, message: '选题不能为空' }],
		selectionConfig: [{ required: true, message: '选题配置不能为空' }],
		examTimeLimit: [{ required: true, message: '考试时间不能为空' }],
		status: [{ required: true, message: '状态不能为空' }],
		onlyForExamHall: [{ required: true, message: '是否仅用于考场不能为空' }]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				tblExamPaperApi
					.tblExamPaperSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
